<template>
    <div>
        <h2>这是角色添加界面</h2>
        <el-form ref="ruleFormRef" style="max-width: 350px;margin-left: 400px;" :model="form" :rules="rules"
            label-width="auto" class="demo-ruleForm" :size="formSize" status-icon>
            <el-form-item label="角色名称" prop="roleName">
                <el-input v-model="form.roleName" />
            </el-form-item>
            <el-form-item>
                <el-button type="primary" @click="submitForm(ruleFormRef)">
                    添加
                </el-button>
            </el-form-item>
        </el-form>
    </div>

</template>

<script setup lang="ts">
import { onMounted, reactive, ref } from 'vue'
import { ElMessage, type ComponentSize, type FormInstance, type FormRules } from 'element-plus'
import axios from 'axios';
import { useRouter } from 'vue-router';
import { useCounterStore } from '@/stores/counter';

const store = useCounterStore();
const router = useRouter();

const formSize = ref<ComponentSize>('default')
const ruleFormRef = ref<FormInstance>()
const form = reactive({
    roleName: "",
    createName: store.ccstore.userName,
    createTime: new Date(),
    updateName: null,
    updateTime: null,
    isDel: false
})
const rules = reactive<FormRules>({
    roleName: [
        { required: true, message: '角色名称不能为空', trigger: 'blur' },
    ],
})
const submitForm = async (formEl: FormInstance | undefined) => {
    if (!formEl) return
    await formEl.validate((valid, fields) => {
        if (valid) {
            axios.post("https://localhost:7272/api/Message/AddRole", form).then(res => {
                if (res.data.code == 100) {
                console.log(res.data);
                    ElMessage.success(res.data.msg);
                    router.push("/NewRoleListView")
                   /*  router.push({
                        name: 'RoleList'
                    }) */
                } else {
                    if (res.data.data == 0) {
                        ElMessage.error("角色名称不能重复");
                        return
                    }
                    else {
                        ElMessage.error("添加失败");
                        return
                    }

                }
            })
        } else {
            console.log('error submit!', fields)
        }
    })
}
</script>
